package com.example.shangjiqi;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.shangjiqi.R;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "LoginActivity";

    private EditText etUsername;
    private EditText etPassword;
    private CheckBox cbAgreement;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_login);

        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        cbAgreement = findViewById(R.id.cb_agreement);
        btnLogin = findViewById(R.id.btn_login);

        // 用户名输入框：获取焦点时边框变亮，失去焦点时去除前后空格
        etUsername.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                etUsername.setBackgroundResource(R.drawable.edittext_focused);
            } else {
                etUsername.setBackgroundResource(R.drawable.edittext_normal);
                String username = etUsername.getText().toString().trim();
                etUsername.setText(username);
            }
        });

        // 密码输入框：获取焦点时边框变亮
        etPassword.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                etPassword.setBackgroundResource(R.drawable.edittext_focused);
            } else {
                etPassword.setBackgroundResource(R.drawable.edittext_normal);
            }
        });

        // 协议复选框：控制登录按钮可用状态
        cbAgreement.setOnCheckedChangeListener((buttonView, isChecked) -> {
            btnLogin.setEnabled(isChecked);
            btnLogin.setBackgroundColor(isChecked ?
                    getResources().getColor(R.color.blue) :
                    getResources().getColor(R.color.gray));
        });

        // 登录按钮点击事件
        btnLogin.setOnClickListener(v -> {
            String username = etUsername.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            // 验证输入
            if (username.isEmpty()) {
                Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
                return;
            }
            if (password.isEmpty()) {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!cbAgreement.isChecked()) {
                Toast.makeText(this, "请同意用户协议", Toast.LENGTH_SHORT).show();
                return;
            }

            // 模拟登录：输入"test"则跳转首页
            if ("test".equals(username)) {
                Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                intent.putExtra("username", username);
                startActivity(intent);
                finish(); // 关闭登录页，避免返回
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "用户名错误（仅\"test\"可演示）", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }
}